<?php
	$ls_gridname	= 'dtl_sample2';


// ----------------- >> Form Detail << ----------------- ?>
<script type="text/javascript">
	$(document).ready(function(){
		
		// Setup Form
		$('#form_<?php echo $ls_gridname; ?>').setupForm([
			{ name: 'index_no', type:'numeric', precision:0 },
			{ name: 'dtl_ref_key_combo', size: 'long', data:<?php echo json_encode($arr_reference); ?> },
			{ name: 'ref_key_lov', type: 'lov', size: 'short',
				onClearButton:function(){
					$(this).setval(''); 
					$('#<?php echo $ls_gridname; ?>_ref_name_lov').setval(''); 
				},
				onActionButton:function(){ 
					LOVCoreSampleReference({ onSelect:function(row){ 
						$('#<?php echo $ls_gridname; ?>_ref_key_lov').setval(row.ref_key); 
						$('#<?php echo $ls_gridname; ?>_ref_name_lov').setval(row.ref_name); 
					} }); 
				}
			},
			{ name: 'ref_name_lov', type: 'text', size: '24', readonly:true },
			{ name: 'jumlah', type: 'numeric', precision:2 },
			{ name: 'keterangan', type: 'text', size: 'medium', maxlength:100 }
		], {}, { prefix:'<?php echo $ls_gridname; ?>_' });
		
		// Set Hidden Object
		$('#<?php echo $ls_gridname; ?>_index_no').hide();
		
		// Create Dialog
		$('#form_<?php echo $ls_gridname; ?>_container').dialog({
			closed:true,
			modal:true,
			title: '_',
			width:550,
			height: 300,
			buttons:[{
				text:'Save',
				iconCls:'icon-save',
				handler:function(){
					var ls_mode			= $('#form_<?php echo $ls_gridname; ?>').data('mode');
					var row				= formToJSON('form_<?php echo $ls_gridname; ?>');
					var ls_error		= '';
					
					row.ref_key_combo = row.dtl_ref_key_combo;
					
					// Validasi
					if(row.ref_key_combo == ''){
						ls_error	+= 'Isi Ref Combo\n';
					}
					if(row.ref_key_lov == ''){
						ls_error	+= 'Isi Ref LOV\n';
					}
					if(row.keterangan == ''){
						ls_error	+= 'Isi Keterangan\n';
					}
					
					// Set Combo Name
					row.ref_name_combo	= $('#<?php echo $ls_gridname; ?>_dtl_ref_key_combo :selected').text();
					
					// Eksekusi
					if(ls_error!=''){
						$.messager.alert('Error', nl2br(ls_error), 'error');
					}else{
						if(ls_mode=='create'){
							$('#<?php echo $ls_gridname; ?>').datagrid('appendRow', row );
							$('#form_<?php echo $ls_gridname; ?>_container').dialog('close');
						} else {
							var selectedIndex = $('#<?php echo $ls_gridname; ?>').data('selectedIndex');
							
							updateRowFlag('<?php echo $ls_gridname; ?>', row); // Patching Flag Update
							$('#<?php echo $ls_gridname; ?>').datagrid('updateRow',{index:selectedIndex,row:row});
							
							$('#form_<?php echo $ls_gridname; ?>_container').dialog('close');
						}
					}
				}
			},{
				text:'Close',
				iconCls:'icon-cancel',
				handler:function(){
					$('#form_<?php echo $ls_gridname; ?>_container').dialog('close');
				}
			}]
		});
	});
</script>
<div id="form_<?php echo $ls_gridname; ?>_container" style="padding:5px;width:600px;height:400px;">
	<div id="form_<?php echo $ls_gridname; ?>">
    <input id="index_no" />
    <table class="tb_form" style="width:100%;" cellpadding="0" cellspacing="0">
    <tr>
        <td class="label">Ref Combo</td>
        <td><input id="dtl_ref_key_combo" /></td>
    </tr>
    <tr>
        <td class="label">Ref LOV</td>
        <td><input id="ref_key_lov" /><input id="ref_name_lov" /></td>
    </tr>
    <tr>
        <td class="label">Jumlah</td>
        <td><input id="jumlah" /></td>
    </tr>
    <tr>
        <td class="label">Keterangan</td>
        <td><input id="keterangan" /></td>
    </tr>
    </table>
	</div>
</div>
<?php 



// ----------------- >> Table Detail << ----------------- ?>
<script type="text/javascript">
	$(document).ready(function(){
		$('#<?php echo $ls_gridname; ?>').datagrid({
			url:gs_path+'/'+controller+'/<?php echo $ls_gridname; ?>_data',
			queryParams:{ pk_master:$('#pk_master').getval(), rows:9999999999 },
			<?php if($mode!='view'){ 
			// -->> Toolbar [Start]
			// Only Appear On Mode Create / Edit ?>
			toolbar:[{
				text:'Tambah',
				iconCls:'icon-add',
				handler:function(){
					resetError();
					
					$('#form_<?php echo $ls_gridname; ?>_container').dialog('open');
					$('#form_<?php echo $ls_gridname; ?>').data('mode', 'create');
					$('#form_<?php echo $ls_gridname; ?>_container').prev().find('.panel-title').html('Tambah Data Detail 2');
					
					$('#form_<?php echo $ls_gridname; ?>').resetForm();
				}
			},'-',{
				text:'Ubah',
				iconCls:'icon-edit',
				handler:function(){
					var oSel=$('#<?php echo $ls_gridname; ?>').datagrid('getSelected'); 
					if(oSel==null){ $.messager.alert('&nbsp;', gs_msg_row_unselected, 'warning'); }
					else{ 
						resetError();
						
						$('#form_<?php echo $ls_gridname; ?>_container').dialog('open');
						$('#form_<?php echo $ls_gridname; ?>').data('mode', 'edit');
						$('#form_<?php echo $ls_gridname; ?>_container').prev().find('.panel-title').html('Ubah Data Detail 2');
						
						jsonToForm(oSel, {prefix:'<?php echo $ls_gridname; ?>_'});
						$('#<?php echo $ls_gridname; ?>_dtl_ref_key_combo').val( $('#<?php echo $ls_gridname; ?>_ref_key_combo').val() );
					}
				}
			},'-',{
				text:'Hapus',
				iconCls:'icon-remove',
				handler:function(){
					var row = $('#<?php echo $ls_gridname; ?>').datagrid('getSelected');
					if(row==null){ $.messager.alert('&nbsp;', gs_msg_row_unselected, 'warning'); }
					else if(row){
						if(!confirm('Hapus baris data?')) return false;

						var index = $('#<?php echo $ls_gridname; ?>').datagrid('getRowIndex', row);
						$('#<?php echo $ls_gridname; ?>').datagrid('deleteRow', index);
					}
				}
			}],<?php 
			} // -->> Toolbar [End]
			?>
			onClickRow:function(rowIndex){
				var lastIndex = $('#<?php echo $ls_gridname; ?>').data('selectedIndex');
				$('#<?php echo $ls_gridname; ?>').data('selectedIndex', rowIndex);
			},
			onLoadSuccess:function(){ }
		});
	});
	
</script>
<table id="<?php echo $ls_gridname; ?>" style="width:600;height:auto" singleSelect="true" idField="index_no" showFooter="true">
<thead>
    <tr>
    	<th field="index_no" hidden="true"></th>
        <th field="ref_key_combo" hidden="true">Combo Key</th>
        <th field="ref_name_combo" width="200">Combo Name</th>
        <th field="ref_key_lov" width="80">LOV Key</th>
        <th field="ref_name_lov" width="200">LOV Name</th>
        <th field="jumlah" width="120" align="right" formatter="formatNumericPoint2">Jumlah</th>
        <th field="keterangan" width="400" align="left">Keterangan</th>
    </tr>
</thead>
</table>
